﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvOpenDistricts.Criteria;
using MyFloridaHouse.libSvCommonUtilities;

namespace MyFloridaHouse.libSvOpenDistricts.MapShapes
{
    public partial class RegionLabel : RegionLabelBase
    {


        #region criteria properties

        public const string CriteriaSubscriberName = "RegionLabel";

        private string _criteriaDatasetName = "";
        public string criteriaDatasetName
        {
            get { return _criteriaDatasetName; }
            set
            {
                if (_criteriaDatasetName != value)
                {
                    _criteriaDatasetName = value;
                    NotifyPropertyChanged("criteriaDatasetName");
                    RaiseRegionLabelDataChanged();
                }
            }
        }

        private ObservableCollection<CriteriaDatasetDTO> _datasetCritieraList;
        public ObservableCollection<CriteriaDatasetDTO> datasetCriteriaList
        {
            get { return _datasetCritieraList; }
            set
            {
                if (_datasetCritieraList != value)
                {
                    _datasetCritieraList = value;
                    NotifyPropertyChanged("datasetCriteriaList");
                    // don't need to RaiseRegionLabelChanged because it is not a primary driver
                }
            }
        }


        private int _selectedDatasetCriteriaIndex = -1;
        public int selectedDatasetCriteriaIndex
        {
            get { return _selectedDatasetCriteriaIndex; }
            set
            {
                // turn off old selection
                if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
                {
                    datasetCriteriaList[selectedDatasetCriteriaIndex].isSelected = false;
                }

                _selectedDatasetCriteriaIndex = value;

                // turn on new selection
                if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
                {
                    datasetCriteriaList[selectedDatasetCriteriaIndex].isSelected = true;
                }
                CriteriaDatasetManager.UpdateSubscriberDisplayCriteriaIndex(CriteriaSubscriberName, selectedDatasetCriteriaIndex);

                NotifyPropertyChanged("selectedDatasetCriteriaIndex");
                RaiseRegionLabelDataChanged();
            }
        }

        #endregion


        public RegionLabel()
        {
            LoadCriteriaDataset(Constants.DefaultCriteriaDatasetName);
        }

        public void LoadCriteriaDataset(string newCriteriaDatasetName)
        {
            if (this.criteriaDatasetName != newCriteriaDatasetName)
            {
                this.criteriaDatasetName = newCriteriaDatasetName;
                this.datasetCriteriaList = CriteriaDatasetManager.Subscribe(newCriteriaDatasetName, RegionLabel.CriteriaSubscriberName);
            }
        }

        public override UIElement BuildLabel(bool showName, string regionName, List<RegionDataDTO> regionDataList)
        {
            string nameLabel = string.Empty;
            if (showName)
            {
                nameLabel = regionName;
            }

            List<string> labelDataList = new List<string>();
            // find the data value
            int regionComputedDataValue = 0;

            if ((0 <= selectedDatasetCriteriaIndex) & (selectedDatasetCriteriaIndex < datasetCriteriaList.Count))
            {
                if (CriteriaDataset.EvaluateCriteria(regionDataList, datasetCriteriaList[selectedDatasetCriteriaIndex], false, out regionComputedDataValue))
                {
                    if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
                    {

                        labelDataList.Add(regionComputedDataValue.ToString());
                    }
                }
            }

            return GetRegionLabelShape(nameLabel, labelDataList);
        }
    }
}
